package ch.bfh.mobicomp.soda2.gps_datacollector.business;

import android.app.Service;
import android.content.Intent;
import android.location.LocationManager;
import android.os.IBinder;
import android.util.Log;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.listener.SODA_GPS_LocationListener;
import ch.bfh.mobicomp.soda2.gps_datacollector.business.utils.SODA_GPS_SharedPreferenceManager;

public class SODA_GPS_DataCollector_Service extends Service {

    private static final String TAG = SODA_GPS_DataCollector_Service.class.getName();

    private static final String PROVIDER = LocationManager.GPS_PROVIDER;
    private static Intent myIntent = new Intent();

    public static final String MIN_DISTANCE_DELAY_EXTRA_NAME = "MIN_DISTANCE_DELAY";
    public static final String MIN_TIME_DELAY_EXTRA_NAME = "MIN_TIME_DELAY";

    public static final String GPS_ONSTART_INTENT = "GPS_ONSTART_INTENT";
    public static final String GPS_ONSTOP_INTENT = "GPS_ONSTOP_INTENT";
    public static final String GPS_ONRESUME_INTENT = "GPS_ONRESUME_INTENT";

    private LocationManager locationManager;
    private SODA_GPS_LocationListener gpsLocationListener = new SODA_GPS_LocationListener(this);
    private String intentAction = "";
    
    private SODA_GPS_SharedPreferenceManager preferenceManager = SODA_GPS_SharedPreferenceManager.getInstance(this);

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
        if (intent.getAction() != null) {
            intentAction = intent.getAction();
            Log.d(TAG, "IntentAction: " + intentAction);
            if (intentAction.equals(GPS_ONSTART_INTENT)) {
                startServiceRoutine(intent);
            } else if (intentAction.equals(GPS_ONRESUME_INTENT)) {
                onResumeRoutine(intent);
            }
        } else {
            Log.e(TAG, "IntentAction is null");
        }

        return Service.START_STICKY;
    }

    private void onResumeRoutine(Intent intent) {
        
        if (preferenceManager.wasStarted()) {
            if (gpsLocationListener != null) {
                locationManager.removeUpdates(gpsLocationListener);
            }
            setupLocationUpdates(preferenceManager);

        } else {
            Log.d(TAG, "Service was stoped... Do nothing!!!");
        }
    }


    private void startServiceRoutine(Intent intent) {
        if (intent.hasExtra(MIN_DISTANCE_DELAY_EXTRA_NAME) && intent.hasExtra(MIN_TIME_DELAY_EXTRA_NAME)) {
            preferenceManager.saveMinDistanceDelay(intent.getExtras().getFloat(MIN_DISTANCE_DELAY_EXTRA_NAME));
            preferenceManager.saveMinTimeDelay(intent.getExtras().getLong(MIN_TIME_DELAY_EXTRA_NAME));
        }
        setupLocationUpdates(preferenceManager);
    }

    private void setupLocationUpdates(SODA_GPS_SharedPreferenceManager preferenceManager) {
        long minTime = preferenceManager.getMinTimeDelay();
        float minDistance = preferenceManager.getMinDistanceDelay();
        Log.d(TAG, "requestUpdates - minTime: " + minTime + " minDist: " + minDistance);
        locationManager.requestLocationUpdates(PROVIDER, minTime, minDistance, gpsLocationListener);
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onDestroy() {
        locationManager.removeUpdates(gpsLocationListener);
        Log.d(TAG, "onDestroy Service");
        super.onDestroy();
    }

    public static Intent getStartGPSServicetIntent(float distanceDeltaMeter, long timeDeltaMilliSec) {
        myIntent.setAction(GPS_ONSTART_INTENT);
        myIntent.putExtra(MIN_DISTANCE_DELAY_EXTRA_NAME, distanceDeltaMeter);
        myIntent.putExtra(MIN_TIME_DELAY_EXTRA_NAME, timeDeltaMilliSec);
        return myIntent;
    }

    public static Intent getStopGPSServiceIntent() {
        myIntent.setAction(GPS_ONSTOP_INTENT);
        return myIntent;

    }

}
